package org.wevil.repo.sys;

import org.wevil.entity.sys.SysOrg;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.wevil.repo.BaseRepo;

import java.util.List;

public interface SysOrgRepo extends BaseRepo<SysOrg, Integer> {

    @Query("select count(pid) from SysOrg where pid = ?1")
    Integer countByPid(Integer pid);

    @Query("from SysOrg where id in ?1")
    List<SysOrg> findByOrgIdIn(List<Integer> orgIds);

    List<SysOrg> findByPid(Integer orgId);

    @Query("select name from SysOrg where id in ?1")
    List<String> findOrgNameByOrgIdIn(List<Integer> orgIds);

    @Modifying
    @Query("update SysOrg t set " +
            "t.pid = case when :#{#entity.pid} is null then t.pid else :#{#entity.pid} end, " +
            "t.name = case when :#{#entity.name} is null then t.name else :#{#entity.name} end, " +
            "t.sort = case when :#{#entity.sort} is null then t.sort else :#{#entity.sort} end " +
            "where t.id = :#{#entity.id}")
    void update(SysOrg entity);
}
